package com.snu.liwei.lyl_api.service;

import com.snu.liwei.lyl_api.entity.Gene;
import com.snu.liwei.lyl_api.entity.Pattern;
import com.snu.liwei.lyl_api.mapper.GeneMapper;
import com.snu.liwei.lyl_api.tools.FileUtils;
import com.snu.liwei.lyl_api.tools.Xml2JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.List;

@Slf4j
@Service
public class GeneService {

    @Autowired
    GeneMapper geneMapper;

    @Value("${glib.path}")
    private String path;

    public List<Gene> getGeneList()
    {
        return geneMapper.getGeneList();
    }

    public List<Pattern> getPatternList()
    {
        return geneMapper.getPtList();
    }

    public String getGeneDetail(Gene gene){
        Gene myGene=geneMapper.getGeneDetail(gene);
        File file=new File(myGene.getPath());
        String geneJson = Xml2JsonUtil.xml2JSON(file);
        return geneJson;
    }

    public String getPatternDetail(String patternPath){
        return "";
    }

    public Object getGeneByGid(String gid) {
        //获取查询磁盘位置 /usr/local/project/back_end/PatternSet/JSON/GLib
        String filePath = path +gid+".json";
        return FileUtils.readFile(filePath);
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveGeneJson(String gId,String path,String nation,Integer caseCount,String jsonPath) {
        geneMapper.saveGene( gId, path, nation, caseCount, jsonPath);
    }
}
